﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Dijkstra_cs
{
    /// <summary>
    /// Interaction logic for UserControl1.xaml
    /// </summary>
    public partial class UserControl1 : UserControl
    {


        int[,] _L ={
                {-1,  5, -1, -1, -1,  3, -1, -1}, 
                { 5, -1,  2, -1, -1, -1,  3, -1}, 
                {-1,  2, -1,  6, -1, -1, -1, 10}, 
                {-1, -1,  6, -1,  3, -1, -1, -1},
                {-1, -1, -1,  3, -1,  8, -1,  5}, 
                { 3, -1, -1, -1,  8, -1,  7, -1}, 
                {-1,  3, -1, -1, -1,  7, -1,  2}, 
                {-1, -1, 10, -1,  5, -1,  2, -1} 
                    };

        List<Ellipse> _list = new List<Ellipse>();

        Dijkstra _D = null; 

        Polyline _Polyline = new Polyline();


        public UserControl1()
        {
            InitializeComponent();



            _list.Add(this.ellipse1);
            _list.Add(this.ellipse2);
            _list.Add(this.ellipse3);
            _list.Add(this.ellipse4);
            _list.Add(this.ellipse5);
            _list.Add(this.ellipse6);
            _list.Add(this.ellipse7);
            _list.Add(this.ellipse8);


            int[,] point = { { }, { }, { }, { }, { } };


            for (int i = 0; i < 8; i++)
            {
                for (int j = 0; j < 8; j++)
                {
                    if (_L[i, j] != -1)
                    {
                        Line line = new Line();

                        line.X1 = Canvas.GetLeft(_list[i]);
                        line.Y1 = Canvas.GetTop(_list[i]);

                        line.X2 = Canvas.GetLeft(_list[j]);
                        line.Y2 = Canvas.GetTop(_list[j]);



                        line.Stroke = System.Windows.Media.Brushes.LightSteelBlue;


                        canvas1.Children.Add(line);

                        //ppp.Children.Add(line);


                    }
                }

            }



            _D = new Dijkstra(8, _L);

            _D.Run();

            _Polyline.Stroke = System.Windows.Media.Brushes.SlateGray;
            _Polyline.StrokeThickness = 2;

            canvas1.Children.Add(_Polyline);


        }

        private void Grid_MouseMove(object sender, MouseEventArgs e)
        {


            


            //myGrid.Children.Add(myLine);
    

        }

        int GetSelectNode(Point po)
        {
            double len = double.MaxValue;
            int index = 0;

            

            for (int i = 0;i < _list.Count; i++)
            {

                double elen = Math.Abs( (Canvas.GetLeft(_list[i]) -   po.X)) + Math.Abs( (Canvas.GetTop(_list[i]) -   po.Y));

                if (elen < len)
                {
                    index = i;
                    len = elen;
                }

            }
            return index;

        }

        enum NTOE
        {
            A = 0,
            B,
            C,
            D,
            E,
            F,
            G,
            H
        }

        void DrawSelectNode(int index)
        {
            List<int> path = new List<int>();
            _D.get_via(index, path);

            String strPath = string.Empty;



            _Polyline.Points.Clear();

            Point po = new Point();
            po.X = Canvas.GetLeft(_list[0]);
            po.Y = Canvas.GetTop(_list[0]);
            _Polyline.Points.Add(po);

            foreach (int p in path)
            {

                po.X = Canvas.GetLeft(_list[p]);
                po.Y = Canvas.GetTop(_list[p]);
                _Polyline.Points.Add(po);

                strPath +=  (NTOE)p;
                strPath += ">";
            }


            po.X = Canvas.GetLeft(_list[index]);
            po.Y = Canvas.GetTop(_list[index]);
            _Polyline.Points.Add(po);


            strPath += (NTOE)index;


            listBox1.Items.Add(strPath);

            Path pp = new Path();
            



        }

        private void canvas1_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            //listBox1.Items.Add("test");

            Point po = e.GetPosition(canvas1);

            int index = GetSelectNode(po);

            if (index == 0)
                return;
            //listBox1.Items.Add(index);

            DrawSelectNode(index);

        }



    }
}
